/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package cn.stylefeng.guns.modular.oss.controller;

import cn.stylefeng.guns.modular.dms.model.params.DirectoryFileParam;
import cn.stylefeng.guns.modular.dms.service.DirectoryFileService;
import cn.stylefeng.guns.modular.oss.cloud.CloudStorageConfig;
import cn.stylefeng.guns.modular.oss.cloud.OSSFactory;
import cn.stylefeng.guns.utils.Constant;
import cn.stylefeng.guns.utils.FileUtils;
import cn.stylefeng.guns.utils.VideoUtils;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/oss")
public class SysOssController {

	@Autowired
	private DirectoryFileService directoryFileService;

	/**
	 * 上传文件
	 */
	@PostMapping("/upload")
	public ResponseData upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			return ResponseData.error("上传文件不能为空");
		}
		//上传文件
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
		return ResponseData.success(url);
	}

	/**
	 * 上传文件到指定目录下
	 * @param file 文件内容
	 * @param directoryId  目录ID
	 * @param isUser  0-表示平台，1-表示用户端，C端
	 * @param owerId 拥有者ID
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/uploadToDir")
	public ResponseData uploadToDir(MultipartFile file, Integer directoryId, Integer isUser, Integer owerId, HttpServletRequest request) throws Exception {
		if (file.isEmpty()) {
			return ResponseData.error("上传文件不能为空");
		}
		Map<String,Object> map=new HashMap<>();
		//上传文件
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
		String fileName=file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
		String contentType=file.getContentType();
		String type=contentType.substring(0,contentType.lastIndexOf("/"));
		Long size=file.getSize();
		map.put("url",url);

		//保存文件信息
		DirectoryFileParam directoryFile=new DirectoryFileParam();
		directoryFile.setName(fileName);
		directoryFile.setFileSuffix(suffix);
		directoryFile.setFileSize(FileUtils.getFileSizeString(size,"KB"));
		if (type.equals("image")){
			directoryFile.setMediaType(Constant.MediaType.IMAGE.getValue());
		}else if (type.equals("video")){
			directoryFile.setMediaType(Constant.MediaType.VIDEO.getValue());
			//上传封面视频
			String filepath = request.getSession().getServletContext().getRealPath("/") + "files\\";//获取项目路径到webapp
			File file2 = new File(filepath);
			if (!file2.exists()) {//目录不存在就创建
				file2.mkdirs();
			}
			//如果视频文件，获取缩略图，并且上传
			String realpath2 = file2 + "\\" + fileName + ".png";
			File upFile2 = new File(realpath2);
			VideoUtils.fetchFrame(url,realpath2);
			String imgUrl = OSSFactory.build().uploadSuffix(new FileInputStream(upFile2), ".png");
			map.put("videoCover",imgUrl);
			directoryFile.setVideoCover(imgUrl);
		}else if (type.equals("audio")){
			directoryFile.setMediaType(Constant.MediaType.AUIDO.getValue());
		}else{
			directoryFile.setMediaType(Constant.MediaType.OTHERS.getValue());
		}
		directoryFile.setUrl(url);
		if (directoryId!=null){
			directoryFile.setDirectoryId(directoryId);
		}
		if (isUser!=null){
			directoryFile.setIsUser(isUser);
		}
		if (owerId!=null){
			directoryFile.setOwerId(owerId);
		}
		directoryFile.setStorageType(new CloudStorageConfig().getType());
		directoryFileService.add(directoryFile);
		return ResponseData.success(map);
	}

}
